{% extends 'base.attached.class' %}

{% block content %}
var {{ class_name }} = function(coeffs, inters) {

    this.coeffs = coeffs;
    this.inters = inters;

    {% if is_binary %}
    this.predict = function(features) {
        var prob = 0.;
        for (var i = 0, il = this.coeffs.length; i < il; i++) {
            prob += this.coeffs[i] * features[i];
        }
        if (prob + this.inters > 0) {
            return 1;
        }
        return 0;
    };
    {% else %}
    this.predict = function(features) {
        var classIdx = 0,
            classVal = Number.NEGATIVE_INFINITY,
            prob = 0.;
        for (var i = 0, il = this.inters.length; i < il; i++) {
            prob = 0.;
            for (var j = 0, jl = this.coeffs[0].length; j < jl; j++) {
                prob += this.coeffs[i][j] * features[j];
            }
            if (prob + this.inters[i] > classVal) {
                classVal = prob + this.inters[i];
                classIdx = i;
            }
        }
        return classIdx;
    };
    {% endif %}

};

var main = function () {
    if (typeof process !== 'undefined' && typeof process.argv !== 'undefined') {
        if (process.argv.length - 2 !== {{ n_features }}) {
            var IllegalArgumentException = function(message) {
                this.message = message;
                this.name = "IllegalArgumentException";
            }
            throw new IllegalArgumentException("You have to pass {{ n_features }} features.");
        }
    }

    // Features:
    var features = process.argv.slice(2);
    for (var i = 0; i < features.length; i++) {
        features[i] = parseFloat(features[i]);
    }

    // Model data:
    {{ coeffs }}
    {{ inters }}

    // Estimator:
    var clf = new {{ class_name }}(coeffs, inters);

    {% if is_test or to_json %}
    // Get JSON:
    console.log(JSON.stringify({
        "predict": clf.predict(features)
    }));
    {% else %}
    // Get class prediction:
    var prediction = clf.predict(features);
    console.log("Predicted class: #" + prediction);
    {% endif %}
}

if (require.main === module) {
    main();
}
{% endblock %}